//
//  UIViewController+CustomNavigationBarItem.m
//  haid
//
//  Created by 任辰东 on 13-6-9.
//  Copyright (c) 2013年 任辰东. All rights reserved.
//

#import "UIViewController+CustomNavigationBarItem.h"
#import "UIBarButtonItem+NavBtnSpacer.h"

#import "UIButton+TouchAreaInsets.h"

@implementation UIViewController (CustomNavigationBarItem)

#pragma mark - 右按钮

- (void)setNavRightItemWithTitle:(NSString*)title
{
    [self setNavRightItemWithTitle:title titleColor:UIColorFromHex(0x030303)];
}

- (void)setNavRightItemWithTitle:(NSString*)title titleColor:(UIColor *)color
{
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(RightBarItemClick:)];
    
    [button setTintColor:color];
    
    // 按钮事件
    //[button addTarget:self action:@selector(RightBarItemClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 添加到导航条
    self.navigationItem.rightBarButtonItem = button;
}

- (void)setNavRightItemWithImage:(UIImage *)img highlightImg:(UIImage*)himg
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // 按钮背景图片自适应
    UIImage *buttonImage = img;
    UIImage *buttonPressedImage = himg;
    
    // 设置按钮大小
    CGRect buttonFrame = [button frame];
    buttonFrame.size.width = buttonImage.size.width;
    buttonFrame.size.height = buttonImage.size.height;
    [button setFrame:buttonFrame];
    
    // 设置按钮背景
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:buttonPressedImage forState:UIControlStateHighlighted];
    
    // 按钮事件
    [button addTarget:self action:@selector(RightBarItemClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [button setTouchAreaInsets:UIEdgeInsetsMake(15, 14, 15, 15)];
    
    // 添加到导航条
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)setNavRightItemWithImages:(NSArray *)imgs highlightImgs:(NSArray*)himgs space:(CGFloat)space
{
    [self setNavRightItemWithImages:imgs highlightImgs:himgs selectedImgs:nil space:space];
}

- (void)setNavRightItemWithImages:(NSArray *)imgs selectedImgs:(NSArray*)simgs space:(CGFloat)space
{
    [self setNavRightItemWithImages:imgs highlightImgs:nil selectedImgs:simgs space:space];
}

- (void)setNavRightItemWithImages:(NSArray *)imgs highlightImgs:(NSArray*)himgs selectedImgs:(NSArray*)simgs space:(CGFloat)space
{
    NSMutableArray*items=[NSMutableArray new];
    for (int i=0;i<imgs.count;i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag=i;
        
        // 按钮背景图片自适应
        UIImage *buttonImage = [UIImage imageNamed:[imgs objectAtIndex:i]];
        UIImage *buttonPressedImage;
        UIImage *buttonSelectedImage;
        if (himgs)
        {
            buttonPressedImage = [UIImage imageNamed:[himgs objectAtIndex:i]];
        }
        if (simgs) {
            buttonSelectedImage = [UIImage imageNamed:[simgs objectAtIndex:i]];
        }
        
        // 设置按钮大小
        button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
        
        // 设置按钮背景
        [button setImage:buttonImage forState:UIControlStateNormal];
        if (buttonPressedImage) {
            [button setImage:buttonPressedImage forState:UIControlStateHighlighted];
        }
        if (buttonSelectedImage) {
            [button setImage:buttonSelectedImage forState:UIControlStateSelected];
        }
        
        // 按钮事件
        [button addTarget:self action:@selector(RightBarItemClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
        
        //间距
        UIBarButtonItem *spaceBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
        spaceBtn.width = space;
        
        //右边距
        UIBarButtonItem *padding = [UIBarButtonItem negativeSpacerWithWidth:4];
        [items addObject:padding];
        
        [items addObject:barButton];
        [items addObject:spaceBtn];
        
    }
    // 添加到导航条
    self.navigationItem.rightBarButtonItems = items;
}

- (void)RightBarItemClick:(UIButton*)sender
{
    // 右按钮点击触发的事件，实际事件请在在子类重写
}
- (void)homepageClick
{
    
}

#pragma mark - 左按钮

- (void)BackBarItemClick
{
    NSArray *viewControllerArray = [self.navigationController viewControllers];
    if ([viewControllerArray count]>1) {
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }
}

- (void)setNavLeftItemWithTitle:(NSString*)title
{
    [self setNavLeftItemWithTitle:title titleColor:UIColorFromHex(0x030303)];
}

- (void)setNavLeftItemWithTitle:(NSString*)title titleColor:(UIColor *)color
{
    UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(LeftBarItemClick)];
    
    [button setTintColor:color];
    // 按钮事件
    //[button addTarget:self action:@selector(RightBarItemClick:) forControlEvents:UIControlEventTouchUpInside];
    // 添加到导航条
    self.navigationItem.leftBarButtonItem = button;
    [self.navigationItem.leftBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateNormal];
}

- (void)setNavLeftItemWithImage:(UIImage *)img highlightImg:(UIImage*)himg
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // 按钮背景图片自适应
    UIImage *buttonImage = img;
    UIImage *buttonPressedImage = himg;
    
    // 设置按钮大小
    CGRect buttonFrame = [button frame];
    if (img.size.width > 60) {
        buttonFrame.size.width = 90;
    }else{
        buttonFrame.size.width = 50;
    }
    
    buttonFrame.size.height = 44;
    [button setFrame:buttonFrame];
    
    button.imageView.contentMode = UIViewContentModeScaleAspectFill;
    // 设置按钮背景
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:buttonPressedImage forState:UIControlStateHighlighted];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0,0,0)];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
//    if (!himg) {
//        //此处有坑
//         [button addTarget:self action:@selector(LeftBarItemClick) forControlEvents:UIControlEventTouchUpInside];
//        self.navigationItem.leftBarButtonItem = barButton;
//        return;
//    }
//    if (self.navigationItem.leftBarButtonItem) {
//        // 按钮事件
//        [button addTarget:self action:@selector(homepageClick) forControlEvents:UIControlEventTouchUpInside];
//
//        NSMutableArray*items=[NSMutableArray new];
//        //间距
//        UIBarButtonItem *spaceBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
//        spaceBtn.width = 30;
//        
//        [items addObject:self.navigationItem.leftBarButtonItem];
//        [items addObject:spaceBtn];
//        [items addObject:barButton];
//        self.navigationItem.leftBarButtonItems = items;
//    }else{
        // 按钮事件
        [button addTarget:self action:@selector(LeftBarItemClick) forControlEvents:UIControlEventTouchUpInside];

//        //间距
//        UIBarButtonItem *spaceBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
//        spaceBtn.width = 20;
//        self.navigationItem.leftBarButtonItems = @[spaceBtn,barButton];

    self.navigationItem.leftBarButtonItem = barButton;
//    }
}

- (void)setNavLeftItemWithImage:(UIImage *)img highlightImg:(UIImage*)himg title:(NSString *)title
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // 按钮背景图片自适应
    UIImage *buttonImage = img;
//    UIImage *buttonPressedImage = himg;
    
    // 设置按钮大小
    CGRect buttonFrame = [button frame];
    buttonFrame.size.width = buttonImage.size.width + 100;
    buttonFrame.size.height = buttonImage.size.height;
    [button setFrame:buttonFrame];
    
    
    // 设置按钮背景
    [button setImage:buttonImage forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    
//    [button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
    [button setTitle:[NSString stringWithFormat:@" %@",title] forState:UIControlStateNormal];
    
    // 按钮事件
    [button addTarget:self action:@selector(LeftBarItemClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 添加到导航条
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
}

- (void)LeftBarItemClick
{
    // 左按钮点击触发的事件，实际事件请在在子类重写,可能不是回到上一级视图
    
    NSArray *viewControllers = self.navigationController.viewControllers;
    if (viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else if (viewControllers.count==1) {
        [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
    }
}

@end
